# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : task.py
# Time       ：2023/1/11 21:03
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    task 模型类
    task model
"""

from sqlalchemy import func

from conf.config import OrderLinesConfig
from public.base_model import Base, db

DEFAULT_TASK_CONFIG = {
    "is_state": "0",
    "is_corner": "1",
    "retry_time": "3",
    "sleep_time": "1",
    "notice_type": "FAILURE",
    "task_timeout": "120",
    "task_strategy": "RAISE",
    "callback_module": "Email"
}


class Task(Base):
    __tablename__ = 'base_task'

    task_id = db.Column(db.String(255), unique=True, comment='任务ID')
    prev_id = db.Column(db.String(255), comment='前置任务ID')
    next_id = db.Column(db.String(255), comment='后续任务ID')
    task_index_id = db.Column(db.Integer, comment='任务运行顺序ID')
    task_name = db.Column(db.String(50), comment='任务名称')
    method_name = db.Column(db.String(50), comment='任务执行方法名称')
    method_kwargs = db.Column(db.JSON, comment='任务执行参数')
    task_type = db.Column(
        db.Enum('common', 'process_control', 'parallel', 'group', 'sub_process', 'start', 'end'), comment='任务类型')
    task_module = db.Column(db.String(50), comment='任务模块')
    module_version = db.Column(db.String(64), default=OrderLinesConfig.version, comment='模块版本')
    task_config = db.Column(db.JSON, comment='任务配置', default=DEFAULT_TASK_CONFIG)
    result_config = db.Column(db.JSON, comment='结果配置')
    desc = db.Column(db.String(255), comment='任务描述')
    # 关联关系
    process_id = db.Column(db.String(255), db.ForeignKey('base_process.process_id'), comment='流程ID')
    task_instance = db.relationship('TaskInstance', backref='base_task')


class TaskInstance(Base):
    __tablename__ = 'base_task_instance'

    task_instance_id = db.Column(db.String(255), comment='任务运行ID')
    task_index_id = db.Column(db.Integer, comment='任务运行顺序ID')
    task_name = db.Column(db.String(50), comment='任务名称')
    process_name = db.Column(db.String(50), comment='流程名称')
    task_type = db.Column(
        db.Enum('common', 'process_control', 'parallel', 'group', 'sub_process', 'start', 'end'), comment='任务类型')
    task_module = db.Column(db.String(50), comment='任务模块')
    task_config = db.Column(db.JSON, comment='任务配置', default=DEFAULT_TASK_CONFIG)
    method_name = db.Column(db.String(20), comment='任务执行方法名称')
    method_kwargs = db.Column(db.JSON, comment='任务执行参数')
    task_status = db.Column(db.String(20), comment='任务运行实例状态')
    start_time = db.Column(db.DateTime, default=func.now(), comment='任务实例开始时间')
    end_time = db.Column(db.DateTime, comment='任务实例结束时间')
    runner = db.Column(db.String(40), comment='任务执行者')
    runner_id = db.Column(db.String(40), comment='任务执行者ID')
    task_result = db.Column(db.JSON, comment='任务返回结果')
    task_error_info = db.Column(db.JSON, comment='任务运行错误信息')
    result_config = db.Column(db.JSON, comment='结果配置')
    task_desc = db.Column(db.String(255), comment='任务描述')
    mode = db.Column(db.String(10), comment='任务运行模式', default='running')
    logs = db.Column(db.Text, comment='任务实例日志')
    message_id = db.Column(db.String(255), comment='任务队列ID')
    # 关联关系
    process_id = db.Column(db.String(255), db.ForeignKey('base_process.process_id'), comment='流程ID')
    process_instance_id = db.Column(
        db.String(255), db.ForeignKey('base_process_instance.process_instance_id'), comment='流程实例ID')
    task_id = db.Column(db.String(255), db.ForeignKey('base_task.task_id'), comment='任务ID')
